package com.xudow.app.dynamicstate_old.module.follow;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.GsonBuilder;
import com.jude.beam.expansion.BeamBaseActivity;
import com.squareup.picasso.Picasso;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.domain.entity.DynamicComment;
import com.xudow.app.dynamicstate_old.widget.ContainsEmojiEditText;
import com.xudow.app.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;

/**
 * Created by ske on 2016/6/13.
 */
public class ForwardActivity extends BeamBaseActivity {
    @BindView(R.id.image_back)
    ImageView imageBack;
    @BindView(R.id.comment)
    TextView comment;
    @BindView(R.id.edit_comment)
    ContainsEmojiEditText editComment;
    @BindView(R.id.image)
    ImageView image;
    @BindView(R.id.tv_p_name)
    TextView tvPName;
    @BindView(R.id.tv_p_content)
    TextView tvPContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic_forward);
        ButterKnife.bind(this);

        app = (XApplication) getApplication();
        setupView();
    }

    XApplication app;

    Dynamic dynamic;

    private void setupView() {
        dynamic = (Dynamic) getIntent().getSerializableExtra("dynamic");
        if (!TextUtils.isEmpty(dynamic.getImgpath()))
            Picasso.with(this).load(dynamic.getImgpath()).into(image);
        else
            Picasso.with(this).load(R.mipmap.bg).into(image);
        tvPName.setText("@" + dynamic.getUserName());
        tvPContent.setText(dynamic.getContent());

        imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        final DynamicComment originComment = (DynamicComment) getIntent().getSerializableExtra("dynamicComment");
        comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment.setClickable(false);
//                if (TextUtils.isEmpty(editComment.getText().toString().trim())) {
//                    Toast.makeText(ForwardActivity.this, "请填写内容", Toast.LENGTH_SHORT).show();
//                } else {
                if (originComment != null) {
                    dynamic.setpCommentId(originComment.getId());
                }
                dynamic.setType(2);
                dynamic.setpId(dynamic.getId());
                dynamic.setUsertype(app.isStudent() ? 2 : 1);
                dynamic.setpUsertype(dynamic.getUsertype());
                dynamic.setForwardReason(editComment.getText().toString().trim());
                dynamic.setpUserName(dynamic.getUserName());
                dynamic.setpUserProfileId(dynamic.getUserProfileId());
                DynamicModel.getInstance().forwardDynamic(
                        new GsonBuilder()
                                .excludeFieldsWithoutExposeAnnotation()
                                .create()
                                .toJson(dynamic), dynamic.getId())
//                        .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                        .subscribe(new Subscriber<Integer>() {
                            @Override
                            public void onCompleted() {
                                comment.setClickable(true);
                            }

                            @Override
                            public void onError(Throwable e) {
                                comment.setClickable(true);
                            }

                            @Override
                            public void onNext(Integer integer) {
                                setResult(RESULT_OK);
                                Toast.makeText(ForwardActivity.this, "转发成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        });
//                }
            }
        });
        editComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringUtils.isEmpty(s.toString())) {
                    comment.setTextColor(Color.GRAY);
                } else {
                    comment.setTextColor(Color.RED);
                }
            }
        });
    }
}
